from flask import Blueprint, render_template

from constant.BlogConstant import BlogConstant
from service.ArticleService import get_service_article_by_uuid, get_article_page_by_publish, get_article_content
from util.LoginUtil import get_login_info

view_article = Blueprint('view_article', __name__)
view_article.url_prefix = '/view/articles'


@view_article.route("/<int:page>")
def get_articles(page):
    page = get_article_page_by_publish(page)
    return render_template('public/style1/index.html', page=page)


@view_article.route("/detail/<uuid>")
def get_article_by_uuid(uuid):
    article = get_service_article_by_uuid(uuid)
    login_info = get_login_info()
    if article is None:
        return "error"
    if login_info == {}:
        login_info['is_login'] = False
    else:
        login_info['is_login'] = True
    article.content = get_article_content(article)
    if article.document_type == BlogConstant.document_markdown_type:
        return render_template('public/style1/article-detail-markdown.html', article=article, login_info=login_info)
    else:
        return render_template('public/style1/article-detail-richtext.html', article=article, login_info=login_info)
